//
//  SubscribeGoodsCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/20.
//

import UIKit

class SubscribeGoodsCell: UITableViewCell {
    lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hexString: "#28272D")
        view.allRound(10)
        return view
    }()
    
    lazy var leftLb: UILabel = {
        let label = UILabel()
        label.text = "  现在不用钱  "
        label.textColor = .white
        label.font = UIFont.pingFangSCRegular(ofSize: 11)
        label.backgroundColor = UIColor(hexString: "#5B5F71")
        label.corners([.layerMaxXMaxYCorner], cornerRadius: 9)
        return label
    }()
    
    
    private lazy var vipImg: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "icon_subscribe_viplogo")
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    lazy var timeLb: UILabel = {
        let label = UILabel()
        label.text = "永久"
        label.textColor = .white
        label.font = UIFont.pingFangSCMedium(ofSize: 16)
        return label
    }()
    
    lazy var sendLb: UILabel = {
        let label = UILabel()
        label.text = ""
        label.textColor = UIColor(hexString: "#823B1A")
        label.font = UIFont.pingFangSCRegular(ofSize: 11)
        label.allRound(12)
        return label
    }()
    
    lazy var moneyLb: UILabel = {
        let label = UILabel()
        label.text = "¥198"
        label.textColor = .white
        label.font = UIFont.pingFangSCRegular(ofSize: 20)
        return label
    }()
    
    lazy var lastMoneyLb: UILabel = {
        let label = UILabel()
        label.text = "¥ 298"
        label.textColor = UIColor(hexString: "#000000", transparency: 0.4)
        label.font = UIFont.pingFangSCRegular(ofSize: 10)
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        self.contentView.addSubviews([backView])
        backView.addSubviews([leftLb, vipImg, timeLb,sendLb, moneyLb, lastMoneyLb])
        
        backView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.leading.trailing.equalToSuperview().inset(14)
            make.height.equalTo(68)
            make.bottom.equalToSuperview()
        }
        
        leftLb.snp.makeConstraints { make in
            make.top.leading.equalToSuperview()
            make.height.equalTo(18)
        }
        
        
        vipImg.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(20)
        }
        
        timeLb.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(vipImg.snp.trailing).offset(8)
        }
        
        sendLb.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(timeLb.snp.trailing).offset(6)
            make.height.equalTo(24)
        }
        
        lastMoneyLb.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-14)
            make.bottom.equalTo(moneyLb.snp.bottom).offset(-4)
        }
        
        moneyLb.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(lastMoneyLb.snp.leading).offset(-2)
        }
        
        
        // 创建一个NSAttributedString，设置删除线属性
        let attributedString = NSMutableAttributedString(string: "¥ 298")
        attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedString.length))
        // 将带有删除线的NSAttributedString赋值给UILabel
        lastMoneyLb.attributedText = attributedString
    }
    
    func createAttributedText(money:String) {
        // 创建 NSMutableAttributedString
        let attributedText = NSMutableAttributedString(string: "   赠送\(money)钻石  ")
        // 创建并添加图片
        if let image = UIImage(named: "icon_mine_diamond") { // 使用系统图标作为示例
            let imageAttachment = NSTextAttachment()
            imageAttachment.image = image
            imageAttachment.bounds = CGRect(x: 0, y: -3, width: 18, height: 14) // 调整图片大小和位置
            attributedText.insert(NSAttributedString(attachment: imageAttachment), at: 2)
        }
        self.sendLb.attributedText = attributedText
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
